package com.learn.project2.service.admin.issue;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.project2.bean.MarketIssue;
import com.learn.project2.bean.MarketIssueExample;
import com.learn.project2.mapper.MarketIssueMapper;
import com.learn.project2.model.bo.admin.issue.IssueCreateBO;
import com.learn.project2.model.bo.admin.issue.IssueListBO;
import com.learn.project2.model.bo.admin.issue.IssueUpdateBO;
import com.learn.project2.model.vo.admin.issue.IssueCreateVO;
import com.learn.project2.model.vo.admin.issue.IssueListVO;
import com.learn.project2.model.vo.admin.issue.IssueUpdateVO;
import com.learn.project2.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * \* Author: Valley
 * \* Date: 2022/1/7
 * \* Time: 19:06
 * \* Description:
 */
@Service
public class IssueServiceImpl implements IssueService {
    
    @Autowired
    MarketIssueMapper issueMapper;
    
    /** 
     * @description: 通用问题-查询
     * @param: [listBO] 
     * @return: com.learn.project2.model.vo.admin.issue.IssueListVO 
     * @author 王力
     * @date: 2022-01-07 19:13:59
     */ 
    @Override
    public IssueListVO issueList(IssueListBO listBO) {
        PageHelper.startPage(listBO.getPage(),listBO.getLimit());
        MarketIssueExample issueExample = new MarketIssueExample();
        MarketIssueExample.Criteria criteria = issueExample.createCriteria().andDeletedEqualTo(false);
        if(listBO.getQuestion() != null) criteria.andQuestionLike("%"+listBO.getQuestion()+"%");
        List<MarketIssue> marketIssues = issueMapper.selectByExample(issueExample);
        PageInfo<MarketIssue> pageInfo = new PageInfo<>(marketIssues);
        IssueListVO issueListVO = new IssueListVO();
        issueListVO.setPage(listBO.getPage());
        issueListVO.setLimit(listBO.getLimit());
        issueListVO.setList(pageInfo.getList());
        issueListVO.setPages(pageInfo.getPages());
        issueListVO.setTotal(pageInfo.getTotal());
        return issueListVO;
    }

    /** 
     * @description: 通用问题-添加
     * @param: [createBO] 
     * @return: com.learn.project2.model.vo.admin.issue.IssueCreateVO 
     * @author 王力
     * @date: 2022-01-07 19:26:40
     */ 
    @Override
    public IssueCreateVO issueCreate(IssueCreateBO createBO) {
        MarketIssue marketIssue = new MarketIssue();
        marketIssue.setAnswer(createBO.getAnswer());
        marketIssue.setQuestion(createBO.getQuestion());
        marketIssue.setId(null);
        marketIssue.setAddTime(StringUtils.dateToDate(new Date()));
        marketIssue.setUpdateTime(StringUtils.dateToDate(new Date()));
        int insert = issueMapper.insert(marketIssue);

        MarketIssueExample issue = new MarketIssueExample();
        issue.createCriteria()
                .andAddTimeEqualTo(marketIssue.getAddTime())
                .andUpdateTimeEqualTo(marketIssue.getUpdateTime());
        List<MarketIssue> marketIssues = issueMapper.selectByExample(issue);

        IssueCreateVO issueCreateVO = new IssueCreateVO();
        issueCreateVO.setId(marketIssues.get(0).getId());
        issueCreateVO.setAnswer(marketIssue.getAnswer());
        issueCreateVO.setQuestion(marketIssue.getQuestion());
        issueCreateVO.setAddTime(StringUtils.dateToString(marketIssue.getAddTime()));
        issueCreateVO.setUpdateTime(StringUtils.dateToString(marketIssue.getUpdateTime()));

        return issueCreateVO;

    }

    /**
     * @description: 通用问题-修改
     * @param: [issueUpdateBO]
     * @return: com.learn.project2.model.vo.admin.issue.IssueUpdateVO
     * @author 王力
     * @date: 2022-01-07 19:54:03
     */
    @Override
    public IssueUpdateVO issueUpdate(IssueUpdateBO issueUpdateBO) throws ParseException {
        MarketIssue marketIssue = new MarketIssue();
        marketIssue.setId(issueUpdateBO.getId());
        SimpleDateFormat sdf = new SimpleDateFormat();
        marketIssue.setAddTime(issueUpdateBO.getAddTime());
        marketIssue.setUpdateTime(issueUpdateBO.getUpdateTime());
        marketIssue.setAnswer(issueUpdateBO.getAnswer());
        marketIssue.setQuestion(issueUpdateBO.getQuestion());
        int i = issueMapper.updateByPrimaryKeySelective(marketIssue);

        IssueUpdateVO updateVO = new IssueUpdateVO();
        updateVO.setId(marketIssue.getId());
        updateVO.setAddTime(marketIssue.getAddTime().toString());
        updateVO.setAnswer(marketIssue.getAnswer());
        updateVO.setQuestion(marketIssue.getQuestion());
        updateVO.setUpdateTime(marketIssue.getUpdateTime().toString());

        return updateVO;
    }

    /**
     * @description: 通用问题-删除
     * @param: [id]
     * @return: java.lang.Integer
     * @author 王力
     * @date: 2022-01-07 19:57:50
     */
    @Override
    public Integer issueDelete(Integer id) {
        int i = issueMapper.deleteByPrimaryKey(id);
        return null;
    }
}
